<template>
    <div class="login_container">
        <div class="login_main">
            <div class="l_m_center">
                <div class="header">
                    <img src="@/static/images/userLogin.png" alt="">
                </div>
                <div class="login_form">
                    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="70px"
                             class="demo-dynamic" :rules="rules" :label-position="'left'">
                        <div class="form_item">
                            <el-form-item prop="phone" label="QQ号">
                                <el-input v-model="dynamicValidateForm.phone" placeholder="请输入QQ号"></el-input>
                            </el-form-item>
                        </div>
                        <div class="form_item">
                            <el-form-item label="密码" prop="pass" class="pwd_item">
                                <el-input type="password" v-model="dynamicValidateForm.pass" autocomplete="off"
                                          placeholder="请输入密码" v-if="!isShowPwd">
                                </el-input>
                                <el-input type="text" v-model="dynamicValidateForm.pass" autocomplete="off"
                                          placeholder="请输入密码" v-else>
                                </el-input>
                                <span class="showSwitch" @click="showPwd">
                      <svg-icon :iconClass="isShowPwd?'eye2':'eye'"></svg-icon>
                    </span>
                            </el-form-item>
                            <router-link to="/" tag="p" class="store_register">免费注册</router-link>
                        </div>
                    </el-form>
                    <div class="btn_container">
                        <el-button type="primary" @click="submitForm('dynamicValidateForm')">
                            登录</el-button>
                    </div>
                    <div class="agree">
                        <p>登录即同意并遵守《飞鱼科技协议》</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import '@/icons/svg/form.svg'
    import {login} from "@/api/user";
    export default {
        name: 'login',
        data() {
            const validateName = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入账号'));
                }
                return callback();
            };
            const validatePass = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入密码'));
                }
                return callback();
            };
            return {
                dynamicValidateForm: {
                    phone: '',
                    pass: '',
                },
                isShowPwd: false,
                rules: {
                    phone: [
                        { validator: validateName, trigger: 'blur' },
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' },
                    ],
                },
            };
        },
        rules: {},
        methods: {
            async submitForm() {
                if (this.dynamicValidateForm.phone === '' || this.dynamicValidateForm.pass === '') {
                    this.$message({
                        type: 'error',
                        message: '请将信息填写完整'
                    });
                } else {
                    await login(this.dynamicValidateForm.phone,this.dynamicValidateForm.pass,'qq');
                    this.$message({
                        type: 'error',
                        message: '你输入的账号或密码不正确，请重新输入'
                    });
                }
            },
            showPwd() {
                if (this.dynamicValidateForm.pass === '') {
                    console.log('密码不能为空');
                    return;
                }
                this.isShowPwd = !this.isShowPwd;
            },
        },
    };
</script>

<style scoped>
    .login_container{
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
    }
    .login_main{
        width: 100%;
    }
    .l_m_center{
        width: 600px;
        margin: 80px auto;
        border: 1px solid #ccc;
        border-radius: 6px;
        padding-bottom: 60px;
    }
    .header{
        text-align: center;
        border-bottom: 1px solid #eee;
        padding: 20px 0;
    }
    .login_form{
        padding: 40px 38px 40px;
    }
    .btn_container{
        text-align: center;
    }
    .el-button{
        width: 35%;
    }
    .pwd_item{
        margin-bottom: 0;
    }
    .showSwitch{
        position: absolute;
        right: 10px;
        top: 0;
        cursor: pointer;
    }
    .store_register{
        line-height: 14px;
        font-size: 12px;
        text-align: right;
        margin-top: 20px;
        cursor: pointer;
    }
    .agree{
        text-align: center;
        line-height: 40px;
    }
</style>
